<template>
	<view>
		<view class="container" v-if="listdata[0]">
			<view class="container-box">
				<view class="top-text">为了您和您的家属有一个舒适安全的小区居住环境，可选择对象在出入小区或者在小区内活动位置变化时通知自己。</view>
				<view class="card-box">
					<view class="card" v-for="(item,index) in listdata" :key="index">
						<view class="name">{{item.name}}</view>
						<switch @change="switch1Change($event,item.pigcms_id)" :checked="((item.notice.is_notice==0 || is_check_all) && no_check) ? true : false" style="transform:scale(0.7)" />
					</view>
				</view>
			</view>
			<view class="bottom-box">
				<view class="all-btn">全选</view>
				<switch @change="switch2Change" :checked="is_check_all" style="transform:scale(0.7)" />
			</view>
		</view>
		<view class="tip_box_no_data" v-if="!listdata[0]">
			<image class="no_data_img" src="../../../static/images/no_data.png"></image>
			<view class='no_tip_content'>暂无绑定家属</view>
		</view>
	</view>
</template>

<script>
	var common = require('../../../utils/common.js');
	var app = getApp();
	var _this;
	//这是开始加载的数据
	const getvisitorList = () => {
		common.post('HouseApi&a=notice_family', {
			village_id: _this.village_id,
			pigcms_id: _this.pigcms_id,
		}, data => {
			console.log('++++', data)
			// _this.numlist = data.now_village_bind;
			_this.listdata = data.info;
			if (data.now_village_bind && data.now_village_bind.is_all_notice==0) {
				_this.is_check_all = true
			} else {
				_this.is_check_all = false
			}
		});
	};
	//这是设置的接口
	const getsetNotice = () => {
		common.post('HouseApi&a=notice_set', {
			village_id: _this.village_id,
			pigcms_id: _this.pigcms_id,
			is_notice: _this.is_notice,
			type: _this.type,
			add_pigcms_id: _this.add_pigcms_id
		}, data => {
			console.log('++++', data)
			if (data && data.type!='all') {
				_this.is_check_all = data.is_all_notice
			}
			
		});
	};
	
	export default {
		data() {
			return {
				listdata: {},
				numlist:{},
				village_id: 0,
				pigcms_id: 0,
				actcolor: '',
				btn_style: '',
				mobile_head_color: '',
				type:'',
				add_pigcms_id:0,
				is_notice:0,
				is_check_all: true,
				no_check: true
			}
		},
		onShow() {
			// #ifdef H5
			common.hideWxShare()
			// #endif
		},
		onLoad(options) {
			_this = this;
			// #ifdef H5
			console.log('准备跳小程序页', _this.id);
			common.webToMiniapp(_this.route, options, 'redirect')
			// #endif

			common.setMainColor(_this, function(config) {
				_this.mobile_head_color = config.config.mobile_head_color
				_this.actcolor = config.config.mobile_head_color
				_this.btn_style = 'background-color: ' + _this.actcolor + ';box-shadow:0px 30rpx 30rpx -30rpx ' + _this.actcolor +
					';';

				const res = uni.getStorageSync('village_key');
				if (res) {
					_this.village_id = res.village_id;
					_this.pigcms_id = res.pigcms_id;
					_this.village_name = res.village_name;
				};
				getvisitorList()
			});
		},
		methods: {
			//单选设置
			switch1Change(e,pigcms_id){
				console.log(e)
				_this.add_pigcms_id = pigcms_id
				_this.type = "single" //这是后台设置的单选状态 一个接口就能用于单选和全选设置
				_this.is_notice = e.target.value ? 1 : 0;
				if (!e.target.value) {
					_this.is_check_all = false
				} 
				getsetNotice()
			},
			//全选设置
			switch2Change(e){
				_this.type = "all"
				_this.is_notice = e.target.value ? 1 : 0;
				console.log(e)
				if (!e.target.value) {
					_this.no_check = false
					_this.is_check_all = false
				} else {
					_this.no_check = true
					_this.is_check_all = true
				}
				getsetNotice()
			},
		},
		components: {
			
		}
	}
</script>

<style>
	page {
		background: rgb(255, 255, 255);
	}

	.container {
		margin: 0;
		padding: 0;
		background: rgb(245, 245, 245);
		padding-top: 15rpx;
	}
	.container-box {
		background: #FFFFFF;
		padding: 50rpx 30rpx;
	}
	.top-text {
		margin: 30rpx 30rpx 0rpx 30rpx;
		font-size: 24rpx;
		font-weight: normal;
		font-stretch: normal;
		line-height: 44rpx;
		letter-spacing: 0rpx;
		color: #ababab;
	}
	.card-box {
		margin: 0rpx 30rpx 30rpx 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		flex-wrap: wrap;
	}
	.card {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		width: 245rpx;
		margin-top: 30rpx;
		background: rgb(245, 245, 245);
		border-radius: 15rpx;
		padding: 30rpx 15rpx 30rpx 30rpx;
	}
	.name {
		font-size: 28rpx;
		font-weight: 700;
		font-stretch: normal;
		line-height: 60rpx;
		letter-spacing: 0rpx;
		color: #000000;
	}
	.bottom-box {
		position: fixed;
		left: 0rpx;
		bottom: 50rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		background-color: #bbd8ff;
		margin: 0rpx 30rpx;
		padding: 15rpx 15rpx 15rpx 30rpx;
		width: 634rpx;
		border-radius: 45rpx;
	}
	.all-btn {
		font-size: 28rpx;
		font-weight: normal;
		font-stretch: normal;
		line-height: 60rpx;
		letter-spacing: 0rpx;
		color: #ffffff;
	}
	.tip_box_no_data {
		padding-top: calc((100vh - 80rpx - 288rpx - 21rpx)/2);
		font-size: 28rpx;
		text-align: center;
	}
	.tip_box_no_data .tip_title {
		width: 100%;
		text-align: center;
	}
	.no_data_img{
		width:374rpx;
		height:288rpx;
	}
</style>
